﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;


namespace Sales.DataAccess.DO
{
   public class DOBase<T> where T: class
    {
       protected ResortEntities context;

       public DOBase()
       {
           context = new ResortEntities();
       }

       public void Add(params T[] items)
       {
           foreach (T item in items)
           {
               context.CreateObjectSet<T>().AddObject(item);
           }
           context.SaveChanges();
       }

       public void Update(params T[] items)
       {
           foreach (T item in items)
           {
               context.CreateObjectSet<T>().Attach(item);
               context.ObjectStateManager.ChangeObjectState(item, System.Data.EntityState.Modified);
           }
           context.SaveChanges();
       }

       public void Remove(params T[] items)
       {
           foreach (T item in items)
           {
               context.CreateObjectSet<T>().DeleteObject(item);
           }
           context.SaveChanges();
       }

       public IEnumerable<T> GetAll()
       {
           return context.CreateObjectSet<T>().ToList();
       }

       public IEnumerable<T> SearchBy(Expression<Func<T, bool>> predicate)
       {
           return context.CreateObjectSet<T>().Where(predicate).ToList();
       }


       public T GetSingle(Expression<Func<T, bool>> predicate)
       {
           return context.CreateObjectSet<T>().FirstOrDefault(predicate);
       }
    }
}
